{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1数据加载\n",
    "## 2数据探索与预处理\n",
    "## 3特征和标签选择\n",
    "## 4模型预测xgboost和lightgbm单独预测\n",
    "## 5模型(xgboost和lightgbm按照MAE融合)\n",
    "## 6提交比赛结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导包\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1数据加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID name regDate model brand bodyType fuelType gearbox power kilometer notRepairedDamage regionCode seller offerType creatDate price v_0 v_1 v_2 v_3 v_4 v_5 v_6 v_7 v_8 v_9 v_10 v_11 v_12 v_13 v_14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0 736 20040402 30.0 6 1.0 0.0 0.0 60 12.5 0.0 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1 2262 20030301 40.0 1 2.0 0.0 0.0 0 15.0 - 43...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2 14874 20040403 115.0 15 1.0 0.0 0.0 163 12.5...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3 71865 19960908 109.0 10 0.0 0.0 1.0 193 15.0...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4 111080 20120103 110.0 5 1.0 0.0 0.0 68 5.0 0...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>149995 163978 20000607 121.0 10 4.0 0.0 1.0 16...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>149996 184535 20091102 116.0 11 0.0 0.0 0.0 12...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>149997 147587 20101003 60.0 11 1.0 1.0 0.0 90 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>149998 45907 20060312 34.0 10 3.0 1.0 0.0 156 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>149999 177672 19990204 19.0 28 6.0 0.0 1.0 193...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       SaleID name regDate model brand bodyType fuelType gearbox power kilometer notRepairedDamage regionCode seller offerType creatDate price v_0 v_1 v_2 v_3 v_4 v_5 v_6 v_7 v_8 v_9 v_10 v_11 v_12 v_13 v_14\n",
       "0       0 736 20040402 30.0 6 1.0 0.0 0.0 60 12.5 0.0 ...                                                                                                                                                      \n",
       "1       1 2262 20030301 40.0 1 2.0 0.0 0.0 0 15.0 - 43...                                                                                                                                                      \n",
       "2       2 14874 20040403 115.0 15 1.0 0.0 0.0 163 12.5...                                                                                                                                                      \n",
       "3       3 71865 19960908 109.0 10 0.0 0.0 1.0 193 15.0...                                                                                                                                                      \n",
       "4       4 111080 20120103 110.0 5 1.0 0.0 0.0 68 5.0 0...                                                                                                                                                      \n",
       "...                                                   ...                                                                                                                                                      \n",
       "149995  149995 163978 20000607 121.0 10 4.0 0.0 1.0 16...                                                                                                                                                      \n",
       "149996  149996 184535 20091102 116.0 11 0.0 0.0 0.0 12...                                                                                                                                                      \n",
       "149997  149997 147587 20101003 60.0 11 1.0 1.0 0.0 90 ...                                                                                                                                                      \n",
       "149998  149998 45907 20060312 34.0 10 3.0 1.0 0.0 156 ...                                                                                                                                                      \n",
       "149999  149999 177672 19990204 19.0 28 6.0 0.0 1.0 193...                                                                                                                                                      \n",
       "\n",
       "[150000 rows x 1 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据加载\n",
    "train_data=pd.read_csv('used_car_train_20200313.csv')\n",
    "train_data#读取数据草率了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID</th>\n",
       "      <th>name</th>\n",
       "      <th>regDate</th>\n",
       "      <th>model</th>\n",
       "      <th>brand</th>\n",
       "      <th>bodyType</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>gearbox</th>\n",
       "      <th>power</th>\n",
       "      <th>kilometer</th>\n",
       "      <th>...</th>\n",
       "      <th>v_5</th>\n",
       "      <th>v_6</th>\n",
       "      <th>v_7</th>\n",
       "      <th>v_8</th>\n",
       "      <th>v_9</th>\n",
       "      <th>v_10</th>\n",
       "      <th>v_11</th>\n",
       "      <th>v_12</th>\n",
       "      <th>v_13</th>\n",
       "      <th>v_14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>736</td>\n",
       "      <td>20040402</td>\n",
       "      <td>30.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>60</td>\n",
       "      <td>12.5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.235676</td>\n",
       "      <td>0.101988</td>\n",
       "      <td>0.129549</td>\n",
       "      <td>0.022816</td>\n",
       "      <td>0.097462</td>\n",
       "      <td>-2.881803</td>\n",
       "      <td>2.804097</td>\n",
       "      <td>-2.420821</td>\n",
       "      <td>0.795292</td>\n",
       "      <td>0.914762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2262</td>\n",
       "      <td>20030301</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.264777</td>\n",
       "      <td>0.121004</td>\n",
       "      <td>0.135731</td>\n",
       "      <td>0.026597</td>\n",
       "      <td>0.020582</td>\n",
       "      <td>-4.900482</td>\n",
       "      <td>2.096338</td>\n",
       "      <td>-1.030483</td>\n",
       "      <td>-1.722674</td>\n",
       "      <td>0.245522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>14874</td>\n",
       "      <td>20040403</td>\n",
       "      <td>115.0</td>\n",
       "      <td>15</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>163</td>\n",
       "      <td>12.5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.251410</td>\n",
       "      <td>0.114912</td>\n",
       "      <td>0.165147</td>\n",
       "      <td>0.062173</td>\n",
       "      <td>0.027075</td>\n",
       "      <td>-4.846749</td>\n",
       "      <td>1.803559</td>\n",
       "      <td>1.565330</td>\n",
       "      <td>-0.832687</td>\n",
       "      <td>-0.229963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>71865</td>\n",
       "      <td>19960908</td>\n",
       "      <td>109.0</td>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>193</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.274293</td>\n",
       "      <td>0.110300</td>\n",
       "      <td>0.121964</td>\n",
       "      <td>0.033395</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-4.509599</td>\n",
       "      <td>1.285940</td>\n",
       "      <td>-0.501868</td>\n",
       "      <td>-2.438353</td>\n",
       "      <td>-0.478699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>111080</td>\n",
       "      <td>20120103</td>\n",
       "      <td>110.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>68</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.228036</td>\n",
       "      <td>0.073205</td>\n",
       "      <td>0.091880</td>\n",
       "      <td>0.078819</td>\n",
       "      <td>0.121534</td>\n",
       "      <td>-1.896240</td>\n",
       "      <td>0.910783</td>\n",
       "      <td>0.931110</td>\n",
       "      <td>2.834518</td>\n",
       "      <td>1.923482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>149995</td>\n",
       "      <td>163978</td>\n",
       "      <td>20000607</td>\n",
       "      <td>121.0</td>\n",
       "      <td>10</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>163</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.280264</td>\n",
       "      <td>0.000310</td>\n",
       "      <td>0.048441</td>\n",
       "      <td>0.071158</td>\n",
       "      <td>0.019174</td>\n",
       "      <td>1.988114</td>\n",
       "      <td>-2.983973</td>\n",
       "      <td>0.589167</td>\n",
       "      <td>-1.304370</td>\n",
       "      <td>-0.302592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>149996</td>\n",
       "      <td>184535</td>\n",
       "      <td>20091102</td>\n",
       "      <td>116.0</td>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>125</td>\n",
       "      <td>10.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.253217</td>\n",
       "      <td>0.000777</td>\n",
       "      <td>0.084079</td>\n",
       "      <td>0.099681</td>\n",
       "      <td>0.079371</td>\n",
       "      <td>1.839166</td>\n",
       "      <td>-2.774615</td>\n",
       "      <td>2.553994</td>\n",
       "      <td>0.924196</td>\n",
       "      <td>-0.272160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>149997</td>\n",
       "      <td>147587</td>\n",
       "      <td>20101003</td>\n",
       "      <td>60.0</td>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.233353</td>\n",
       "      <td>0.000705</td>\n",
       "      <td>0.118872</td>\n",
       "      <td>0.100118</td>\n",
       "      <td>0.097914</td>\n",
       "      <td>2.439812</td>\n",
       "      <td>-1.630677</td>\n",
       "      <td>2.290197</td>\n",
       "      <td>1.891922</td>\n",
       "      <td>0.414931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>149998</td>\n",
       "      <td>45907</td>\n",
       "      <td>20060312</td>\n",
       "      <td>34.0</td>\n",
       "      <td>10</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>156</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.256369</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.081479</td>\n",
       "      <td>0.083558</td>\n",
       "      <td>0.081498</td>\n",
       "      <td>2.075380</td>\n",
       "      <td>-2.633719</td>\n",
       "      <td>1.414937</td>\n",
       "      <td>0.431981</td>\n",
       "      <td>-1.659014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>149999</td>\n",
       "      <td>177672</td>\n",
       "      <td>19990204</td>\n",
       "      <td>19.0</td>\n",
       "      <td>28</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>193</td>\n",
       "      <td>12.5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.284475</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.040072</td>\n",
       "      <td>0.062543</td>\n",
       "      <td>0.025819</td>\n",
       "      <td>1.978453</td>\n",
       "      <td>-3.179913</td>\n",
       "      <td>0.031724</td>\n",
       "      <td>-1.483350</td>\n",
       "      <td>-0.342674</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        SaleID    name   regDate  model  brand  bodyType  fuelType  gearbox  \\\n",
       "0            0     736  20040402   30.0      6       1.0       0.0      0.0   \n",
       "1            1    2262  20030301   40.0      1       2.0       0.0      0.0   \n",
       "2            2   14874  20040403  115.0     15       1.0       0.0      0.0   \n",
       "3            3   71865  19960908  109.0     10       0.0       0.0      1.0   \n",
       "4            4  111080  20120103  110.0      5       1.0       0.0      0.0   \n",
       "...        ...     ...       ...    ...    ...       ...       ...      ...   \n",
       "149995  149995  163978  20000607  121.0     10       4.0       0.0      1.0   \n",
       "149996  149996  184535  20091102  116.0     11       0.0       0.0      0.0   \n",
       "149997  149997  147587  20101003   60.0     11       1.0       1.0      0.0   \n",
       "149998  149998   45907  20060312   34.0     10       3.0       1.0      0.0   \n",
       "149999  149999  177672  19990204   19.0     28       6.0       0.0      1.0   \n",
       "\n",
       "        power  kilometer  ...       v_5       v_6       v_7       v_8  \\\n",
       "0          60       12.5  ...  0.235676  0.101988  0.129549  0.022816   \n",
       "1           0       15.0  ...  0.264777  0.121004  0.135731  0.026597   \n",
       "2         163       12.5  ...  0.251410  0.114912  0.165147  0.062173   \n",
       "3         193       15.0  ...  0.274293  0.110300  0.121964  0.033395   \n",
       "4          68        5.0  ...  0.228036  0.073205  0.091880  0.078819   \n",
       "...       ...        ...  ...       ...       ...       ...       ...   \n",
       "149995    163       15.0  ...  0.280264  0.000310  0.048441  0.071158   \n",
       "149996    125       10.0  ...  0.253217  0.000777  0.084079  0.099681   \n",
       "149997     90        6.0  ...  0.233353  0.000705  0.118872  0.100118   \n",
       "149998    156       15.0  ...  0.256369  0.000252  0.081479  0.083558   \n",
       "149999    193       12.5  ...  0.284475  0.000000  0.040072  0.062543   \n",
       "\n",
       "             v_9      v_10      v_11      v_12      v_13      v_14  \n",
       "0       0.097462 -2.881803  2.804097 -2.420821  0.795292  0.914762  \n",
       "1       0.020582 -4.900482  2.096338 -1.030483 -1.722674  0.245522  \n",
       "2       0.027075 -4.846749  1.803559  1.565330 -0.832687 -0.229963  \n",
       "3       0.000000 -4.509599  1.285940 -0.501868 -2.438353 -0.478699  \n",
       "4       0.121534 -1.896240  0.910783  0.931110  2.834518  1.923482  \n",
       "...          ...       ...       ...       ...       ...       ...  \n",
       "149995  0.019174  1.988114 -2.983973  0.589167 -1.304370 -0.302592  \n",
       "149996  0.079371  1.839166 -2.774615  2.553994  0.924196 -0.272160  \n",
       "149997  0.097914  2.439812 -1.630677  2.290197  1.891922  0.414931  \n",
       "149998  0.081498  2.075380 -2.633719  1.414937  0.431981 -1.659014  \n",
       "149999  0.025819  1.978453 -3.179913  0.031724 -1.483350 -0.342674  \n",
       "\n",
       "[150000 rows x 31 columns]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据加载\n",
    "train_data=pd.read_csv('used_car_train_20200313.csv',sep=' ')\n",
    "train_data#这里的数据比较多  我们需要预测的时候车的价格 就是price 这里没有显示出来 让它显示一下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2数据探索与预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['SaleID', 'name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType',\n",
       "       'gearbox', 'power', 'kilometer', 'notRepairedDamage', 'regionCode',\n",
       "       'seller', 'offerType', 'creatDate', 'price', 'v_0', 'v_1', 'v_2', 'v_3',\n",
       "       'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12',\n",
       "       'v_13', 'v_14'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data.columns#就是那个price 我们要预测的目标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data['regDate'].isna().sum()#没有缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "time data '20070009' does not match format '%Y%m%d' (match)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m~/.local/lib/python3.7/site-packages/pandas/core/tools/datetimes.py\u001b[0m in \u001b[0;36m_convert_listlike_datetimes\u001b[0;34m(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)\u001b[0m\n\u001b[1;32m    449\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 450\u001b[0;31m                 \u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconversion\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatetime_to_datetime64\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    451\u001b[0m                 \u001b[0mdta\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDatetimeArray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtz_to_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/tslibs/conversion.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslibs.conversion.datetime_to_datetime64\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: Unrecognized value type: <class 'int'>",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-81-476ea99cfc54>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtemp_regDate\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_datetime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'regDate'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%Y%m%d'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;31m#format='%Y%m%d'表示转成年月日的形式\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mtemp_regDate\u001b[0m\u001b[0;31m#从这个报错 可以看出来 里面已经有了一个异常值20070009  所以需要对异常值的读取进行一下处理\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.7/site-packages/pandas/core/tools/datetimes.py\u001b[0m in \u001b[0;36mto_datetime\u001b[0;34m(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)\u001b[0m\n\u001b[1;32m    801\u001b[0m             \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcache_array\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    802\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m             \u001b[0mvalues\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconvert_listlike\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    804\u001b[0m             \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    805\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mABCDataFrame\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mabc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMutableMapping\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.7/site-packages/pandas/core/tools/datetimes.py\u001b[0m in \u001b[0;36m_convert_listlike_datetimes\u001b[0;34m(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)\u001b[0m\n\u001b[1;32m    452\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mDatetimeIndex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_simple_new\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    453\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mValueError\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 454\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    455\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    456\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.7/site-packages/pandas/core/tools/datetimes.py\u001b[0m in \u001b[0;36m_convert_listlike_datetimes\u001b[0;34m(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)\u001b[0m\n\u001b[1;32m    416\u001b[0m                 \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    417\u001b[0m                     result, timezones = array_strptime(\n\u001b[0;32m--> 418\u001b[0;31m                         \u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexact\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexact\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    419\u001b[0m                     )\n\u001b[1;32m    420\u001b[0m                     \u001b[0;32mif\u001b[0m \u001b[0;34m\"%Z\"\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mformat\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m\"%z\"\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/tslibs/strptime.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslibs.strptime.array_strptime\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: time data '20070009' does not match format '%Y%m%d' (match)"
     ]
    }
   ],
   "source": [
    "temp_regDate=pd.to_datetime(train_data['regDate'],format='%Y%m%d')#format='%Y%m%d'表示转成年月日的形式\n",
    "temp_regDate#从这个报错 可以看出来 里面已经有了一个异常值20070009  所以需要对异常值的读取进行一下处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        2004-04-02\n",
       "1        2003-03-01\n",
       "2        2004-04-03\n",
       "3        1996-09-08\n",
       "4        2012-01-03\n",
       "            ...    \n",
       "149995   2000-06-07\n",
       "149996   2009-11-02\n",
       "149997   2010-10-03\n",
       "149998   2006-03-12\n",
       "149999   1999-02-04\n",
       "Name: regDate, Length: 150000, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp_regDate=pd.to_datetime(train_data['regDate'],format='%Y%m%d',errors='coerce')#errors表示把异常值处理一下\n",
    "temp_regDate#这样就好多了  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19910001"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#因为我们需要看汽车使用了多久 所以需要算一下注册的时间    这个regDate是注册的时间 减去最小值是展示了多久 \n",
    "import numpy as np #creatDate是上下售卖时间 这个减去最小值才是使用的时间\n",
    "np.min(train_data['regDate'])#这个就是注册的时间 19910001  待会儿用这个时间为基准 去算算其他的车展示了多久  \n",
    "#这个时间也是异常的啊  怎么可能会有19910001这个时间呢 00是几月？  最低最低也是01  即 19910101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('1991-01-01 00:00:00')"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#把19910101这个时间设置为 最小的时间 用一个变量保存一下  后面用\n",
    "min_date=pd.to_datetime('19910101',format='%Y%m%d')\n",
    "min_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         4840.0\n",
       "1         4442.0\n",
       "2         4841.0\n",
       "3         2077.0\n",
       "4         7672.0\n",
       "           ...  \n",
       "149995    3445.0\n",
       "149996    6880.0\n",
       "149997    7215.0\n",
       "149998    5549.0\n",
       "149999    2956.0\n",
       "Name: regDate, Length: 150000, dtype: float64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#现在把train_data里面的时间和这个最早的时间值做一个差值  就是汽车的展示时间\n",
    "diff=(temp_regDate-min_date).dt.days\n",
    "diff#这个就是使用了多少天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         4840.0\n",
       "1         4442.0\n",
       "2         4841.0\n",
       "3         2077.0\n",
       "4         7672.0\n",
       "           ...  \n",
       "149995    3445.0\n",
       "149996    6880.0\n",
       "149997    7215.0\n",
       "149998    5549.0\n",
       "149999    2956.0\n",
       "Name: regTime, Length: 150000, dtype: float64"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#这个算是把汽车的展示求出来了  把它放到train_data的dateframe里面去\n",
    "train_data['regTime']=diff\n",
    "train_data['regTime']#已经放进去了   同样的方法 也可以将creatTime算出来 放进去 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        2016-04-04\n",
       "1        2016-03-09\n",
       "2        2016-04-02\n",
       "3        2016-03-12\n",
       "4        2016-03-13\n",
       "            ...    \n",
       "149995   2016-03-27\n",
       "149996   2016-03-12\n",
       "149997   2016-03-28\n",
       "149998   2016-04-01\n",
       "149999   2016-03-05\n",
       "Name: creatDate, Length: 150000, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#把里面的 creatDate 时间值转化为时间戳格式\n",
    "temp_creatDate=pd.to_datetime(train_data['creatDate'],format='%Y%m%d',errors='coerce')\n",
    "temp_creatDate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         9225\n",
       "1         9199\n",
       "2         9223\n",
       "3         9202\n",
       "4         9203\n",
       "          ... \n",
       "149995    9217\n",
       "149996    9202\n",
       "149997    9218\n",
       "149998    9222\n",
       "149999    9195\n",
       "Name: creatTime, Length: 150000, dtype: int64"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data['creatTime']=(temp_creatDate-min_date).dt.days#也减去19910101那个时间 算出来的就是汽车损耗的时间\n",
    "train_data['creatTime']#这样就把汽车的使用时间给放进去了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         4385.0\n",
       "1         4757.0\n",
       "2         4382.0\n",
       "3         7125.0\n",
       "4         1531.0\n",
       "           ...  \n",
       "149995    5772.0\n",
       "149996    2322.0\n",
       "149997    2003.0\n",
       "149998    3673.0\n",
       "149999    6239.0\n",
       "Name: usedTime, Length: 150000, dtype: float64"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同样 creatTime-regTime 得到的就是汽车是使用时间   creatTime代表汽车什么时候上线这个二手车平台  regTime代表汽车的注册时间 二者减法\n",
    "train_data['usedTime']=train_data['creatTime']-train_data['regTime']\n",
    "train_data['usedTime']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SaleID</th>\n",
       "      <th>name</th>\n",
       "      <th>regDate</th>\n",
       "      <th>model</th>\n",
       "      <th>brand</th>\n",
       "      <th>bodyType</th>\n",
       "      <th>fuelType</th>\n",
       "      <th>gearbox</th>\n",
       "      <th>power</th>\n",
       "      <th>kilometer</th>\n",
       "      <th>...</th>\n",
       "      <th>v_5</th>\n",
       "      <th>v_6</th>\n",
       "      <th>v_7</th>\n",
       "      <th>v_8</th>\n",
       "      <th>v_9</th>\n",
       "      <th>v_10</th>\n",
       "      <th>v_11</th>\n",
       "      <th>v_12</th>\n",
       "      <th>v_13</th>\n",
       "      <th>v_14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>200000</td>\n",
       "      <td>133777</td>\n",
       "      <td>20000501</td>\n",
       "      <td>67.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>101</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.236520</td>\n",
       "      <td>0.000241</td>\n",
       "      <td>0.105319</td>\n",
       "      <td>0.046233</td>\n",
       "      <td>0.094522</td>\n",
       "      <td>3.619512</td>\n",
       "      <td>-0.280607</td>\n",
       "      <td>-2.019761</td>\n",
       "      <td>0.978828</td>\n",
       "      <td>0.803322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>200001</td>\n",
       "      <td>61206</td>\n",
       "      <td>19950211</td>\n",
       "      <td>19.0</td>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>73</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.261518</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.120323</td>\n",
       "      <td>0.046784</td>\n",
       "      <td>0.035385</td>\n",
       "      <td>2.997376</td>\n",
       "      <td>-1.406705</td>\n",
       "      <td>-1.020884</td>\n",
       "      <td>-1.349990</td>\n",
       "      <td>-0.200542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>200002</td>\n",
       "      <td>67829</td>\n",
       "      <td>20090606</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>120</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.261691</td>\n",
       "      <td>0.090836</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.079655</td>\n",
       "      <td>0.073586</td>\n",
       "      <td>-3.951084</td>\n",
       "      <td>-0.433467</td>\n",
       "      <td>0.918964</td>\n",
       "      <td>1.634604</td>\n",
       "      <td>1.027173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>200003</td>\n",
       "      <td>8892</td>\n",
       "      <td>20020601</td>\n",
       "      <td>22.0</td>\n",
       "      <td>9</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>58</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.236050</td>\n",
       "      <td>0.101777</td>\n",
       "      <td>0.098950</td>\n",
       "      <td>0.026830</td>\n",
       "      <td>0.096614</td>\n",
       "      <td>-2.846788</td>\n",
       "      <td>2.800267</td>\n",
       "      <td>-2.524610</td>\n",
       "      <td>1.076819</td>\n",
       "      <td>0.461610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>200004</td>\n",
       "      <td>76998</td>\n",
       "      <td>20030301</td>\n",
       "      <td>46.0</td>\n",
       "      <td>6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>116</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.257000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.066732</td>\n",
       "      <td>0.057771</td>\n",
       "      <td>0.068852</td>\n",
       "      <td>2.839010</td>\n",
       "      <td>-1.659801</td>\n",
       "      <td>-0.924142</td>\n",
       "      <td>0.199423</td>\n",
       "      <td>0.451014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49995</th>\n",
       "      <td>249995</td>\n",
       "      <td>111443</td>\n",
       "      <td>20041005</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>150</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.263668</td>\n",
       "      <td>0.000292</td>\n",
       "      <td>0.141804</td>\n",
       "      <td>0.076393</td>\n",
       "      <td>0.039272</td>\n",
       "      <td>2.072901</td>\n",
       "      <td>-2.531869</td>\n",
       "      <td>1.716978</td>\n",
       "      <td>-1.063437</td>\n",
       "      <td>0.326587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49996</th>\n",
       "      <td>249996</td>\n",
       "      <td>152834</td>\n",
       "      <td>20130409</td>\n",
       "      <td>65.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>179</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.255310</td>\n",
       "      <td>0.000991</td>\n",
       "      <td>0.155868</td>\n",
       "      <td>0.108425</td>\n",
       "      <td>0.067841</td>\n",
       "      <td>1.358504</td>\n",
       "      <td>-3.290295</td>\n",
       "      <td>4.269809</td>\n",
       "      <td>0.140524</td>\n",
       "      <td>0.556221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49997</th>\n",
       "      <td>249997</td>\n",
       "      <td>132531</td>\n",
       "      <td>20041211</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>147</td>\n",
       "      <td>12.5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.262933</td>\n",
       "      <td>0.000318</td>\n",
       "      <td>0.141872</td>\n",
       "      <td>0.071968</td>\n",
       "      <td>0.042966</td>\n",
       "      <td>2.165658</td>\n",
       "      <td>-2.417885</td>\n",
       "      <td>1.370612</td>\n",
       "      <td>-1.073133</td>\n",
       "      <td>0.270602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49998</th>\n",
       "      <td>249998</td>\n",
       "      <td>143405</td>\n",
       "      <td>20020702</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>176</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.282106</td>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.067483</td>\n",
       "      <td>0.067526</td>\n",
       "      <td>0.009006</td>\n",
       "      <td>2.030114</td>\n",
       "      <td>-2.939244</td>\n",
       "      <td>0.569078</td>\n",
       "      <td>-1.718245</td>\n",
       "      <td>0.316379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49999</th>\n",
       "      <td>249999</td>\n",
       "      <td>78202</td>\n",
       "      <td>20090708</td>\n",
       "      <td>32.0</td>\n",
       "      <td>8</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.231449</td>\n",
       "      <td>0.103947</td>\n",
       "      <td>0.096027</td>\n",
       "      <td>0.062328</td>\n",
       "      <td>0.110180</td>\n",
       "      <td>-3.689090</td>\n",
       "      <td>2.032376</td>\n",
       "      <td>0.109157</td>\n",
       "      <td>2.202828</td>\n",
       "      <td>0.847469</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>50000 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       SaleID    name   regDate  model  brand  bodyType  fuelType  gearbox  \\\n",
       "0      200000  133777  20000501   67.0      0       1.0       0.0      0.0   \n",
       "1      200001   61206  19950211   19.0      6       2.0       0.0      0.0   \n",
       "2      200002   67829  20090606    5.0      5       4.0       0.0      0.0   \n",
       "3      200003    8892  20020601   22.0      9       1.0       0.0      0.0   \n",
       "4      200004   76998  20030301   46.0      6       0.0       NaN      0.0   \n",
       "...       ...     ...       ...    ...    ...       ...       ...      ...   \n",
       "49995  249995  111443  20041005    4.0      4       0.0       NaN      1.0   \n",
       "49996  249996  152834  20130409   65.0      1       0.0       0.0      0.0   \n",
       "49997  249997  132531  20041211    4.0      4       0.0       0.0      1.0   \n",
       "49998  249998  143405  20020702   40.0      1       4.0       0.0      1.0   \n",
       "49999  249999   78202  20090708   32.0      8       1.0       0.0      0.0   \n",
       "\n",
       "       power  kilometer  ...       v_5       v_6       v_7       v_8  \\\n",
       "0        101       15.0  ...  0.236520  0.000241  0.105319  0.046233   \n",
       "1         73        6.0  ...  0.261518  0.000000  0.120323  0.046784   \n",
       "2        120        5.0  ...  0.261691  0.090836  0.000000  0.079655   \n",
       "3         58       15.0  ...  0.236050  0.101777  0.098950  0.026830   \n",
       "4        116       15.0  ...  0.257000  0.000000  0.066732  0.057771   \n",
       "...      ...        ...  ...       ...       ...       ...       ...   \n",
       "49995    150       15.0  ...  0.263668  0.000292  0.141804  0.076393   \n",
       "49996    179        4.0  ...  0.255310  0.000991  0.155868  0.108425   \n",
       "49997    147       12.5  ...  0.262933  0.000318  0.141872  0.071968   \n",
       "49998    176       15.0  ...  0.282106  0.000023  0.067483  0.067526   \n",
       "49999      0        3.0  ...  0.231449  0.103947  0.096027  0.062328   \n",
       "\n",
       "            v_9      v_10      v_11      v_12      v_13      v_14  \n",
       "0      0.094522  3.619512 -0.280607 -2.019761  0.978828  0.803322  \n",
       "1      0.035385  2.997376 -1.406705 -1.020884 -1.349990 -0.200542  \n",
       "2      0.073586 -3.951084 -0.433467  0.918964  1.634604  1.027173  \n",
       "3      0.096614 -2.846788  2.800267 -2.524610  1.076819  0.461610  \n",
       "4      0.068852  2.839010 -1.659801 -0.924142  0.199423  0.451014  \n",
       "...         ...       ...       ...       ...       ...       ...  \n",
       "49995  0.039272  2.072901 -2.531869  1.716978 -1.063437  0.326587  \n",
       "49996  0.067841  1.358504 -3.290295  4.269809  0.140524  0.556221  \n",
       "49997  0.042966  2.165658 -2.417885  1.370612 -1.073133  0.270602  \n",
       "49998  0.009006  2.030114 -2.939244  0.569078 -1.718245  0.316379  \n",
       "49999  0.110180 -3.689090  2.032376  0.109157  2.202828  0.847469  \n",
       "\n",
       "[50000 rows x 30 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取一下测试集\n",
    "test_data=pd.read_csv('used_car_testB_20200421.csv',sep=' ')\n",
    "test_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "#同理测试集的数据也这样处理一下\n",
    "test_data['regTime']=(pd.to_datetime(test_data['regDate'],format='%Y%m%d',errors='coerce')-min_date ).dt.days\n",
    "test_data['creatTime']=(pd.to_datetime(test_data['creatDate'],format='%Y%m%d',errors='coerce')-min_date ).dt.days\n",
    "test_data['usedTime']=test_data['creatTime']-test_data['regTime']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 150000 entries, 0 to 149999\n",
      "Data columns (total 34 columns):\n",
      " #   Column             Non-Null Count   Dtype  \n",
      "---  ------             --------------   -----  \n",
      " 0   SaleID             150000 non-null  int64  \n",
      " 1   name               150000 non-null  int64  \n",
      " 2   regDate            150000 non-null  int64  \n",
      " 3   model              149999 non-null  float64\n",
      " 4   brand              150000 non-null  int64  \n",
      " 5   bodyType           145494 non-null  float64\n",
      " 6   fuelType           141320 non-null  float64\n",
      " 7   gearbox            144019 non-null  float64\n",
      " 8   power              150000 non-null  int64  \n",
      " 9   kilometer          150000 non-null  float64\n",
      " 10  notRepairedDamage  150000 non-null  object \n",
      " 11  regionCode         150000 non-null  int64  \n",
      " 12  seller             150000 non-null  int64  \n",
      " 13  offerType          150000 non-null  int64  \n",
      " 14  creatDate          150000 non-null  int64  \n",
      " 15  price              150000 non-null  int64  \n",
      " 16  v_0                150000 non-null  float64\n",
      " 17  v_1                150000 non-null  float64\n",
      " 18  v_2                150000 non-null  float64\n",
      " 19  v_3                150000 non-null  float64\n",
      " 20  v_4                150000 non-null  float64\n",
      " 21  v_5                150000 non-null  float64\n",
      " 22  v_6                150000 non-null  float64\n",
      " 23  v_7                150000 non-null  float64\n",
      " 24  v_8                150000 non-null  float64\n",
      " 25  v_9                150000 non-null  float64\n",
      " 26  v_10               150000 non-null  float64\n",
      " 27  v_11               150000 non-null  float64\n",
      " 28  v_12               150000 non-null  float64\n",
      " 29  v_13               150000 non-null  float64\n",
      " 30  v_14               150000 non-null  float64\n",
      " 31  regTime            138653 non-null  float64\n",
      " 32  creatTime          150000 non-null  int64  \n",
      " 33  usedTime           138653 non-null  float64\n",
      "dtypes: float64(22), int64(11), object(1)\n",
      "memory usage: 38.9+ MB\n"
     ]
    }
   ],
   "source": [
    "#看一下train_data的信息\n",
    "train_data.info()#里面notRepairedDamage 是 object 类型看看他是怎么回事儿"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    111361\n",
       "-       24324\n",
       "1.0     14315\n",
       "Name: notRepairedDamage, dtype: int64"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data['notRepairedDamage'].value_counts()#notRepairedDamage字段的意思是 汽车是否还有没有修复的损坏 0代表是 1代表否 -就是没有填\n",
    "#这个时候要把 - 替换掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    135685\n",
       "1.0     14315\n",
       "Name: notRepairedDamage, dtype: int64"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data['notRepairedDamage'].replace('-','0.0',inplace=True)\n",
    "train_data['notRepairedDamage'].value_counts()#这样就把那个-给去掉了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    37224\n",
       "-       8069\n",
       "1.0     4707\n",
       "Name: notRepairedDamage, dtype: int64"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data['notRepairedDamage'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    45293\n",
       "1.0     4707\n",
       "Name: notRepairedDamage, dtype: int64"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data['notRepairedDamage'].replace('-','0.0',inplace=True)\n",
    "test_data['notRepairedDamage'].value_counts()#这样就把那个-给去掉了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 150000 entries, 0 to 149999\n",
      "Data columns (total 34 columns):\n",
      " #   Column             Non-Null Count   Dtype  \n",
      "---  ------             --------------   -----  \n",
      " 0   SaleID             150000 non-null  int64  \n",
      " 1   name               150000 non-null  int64  \n",
      " 2   regDate            150000 non-null  int64  \n",
      " 3   model              149999 non-null  float64\n",
      " 4   brand              150000 non-null  int64  \n",
      " 5   bodyType           145494 non-null  float64\n",
      " 6   fuelType           141320 non-null  float64\n",
      " 7   gearbox            144019 non-null  float64\n",
      " 8   power              150000 non-null  int64  \n",
      " 9   kilometer          150000 non-null  float64\n",
      " 10  notRepairedDamage  150000 non-null  float64\n",
      " 11  regionCode         150000 non-null  int64  \n",
      " 12  seller             150000 non-null  int64  \n",
      " 13  offerType          150000 non-null  int64  \n",
      " 14  creatDate          150000 non-null  int64  \n",
      " 15  price              150000 non-null  int64  \n",
      " 16  v_0                150000 non-null  float64\n",
      " 17  v_1                150000 non-null  float64\n",
      " 18  v_2                150000 non-null  float64\n",
      " 19  v_3                150000 non-null  float64\n",
      " 20  v_4                150000 non-null  float64\n",
      " 21  v_5                150000 non-null  float64\n",
      " 22  v_6                150000 non-null  float64\n",
      " 23  v_7                150000 non-null  float64\n",
      " 24  v_8                150000 non-null  float64\n",
      " 25  v_9                150000 non-null  float64\n",
      " 26  v_10               150000 non-null  float64\n",
      " 27  v_11               150000 non-null  float64\n",
      " 28  v_12               150000 non-null  float64\n",
      " 29  v_13               150000 non-null  float64\n",
      " 30  v_14               150000 non-null  float64\n",
      " 31  regTime            138653 non-null  float64\n",
      " 32  creatTime          150000 non-null  int64  \n",
      " 33  usedTime           138653 non-null  float64\n",
      "dtypes: float64(23), int64(11)\n",
      "memory usage: 38.9 MB\n"
     ]
    }
   ],
   "source": [
    "train_data['notRepairedDamage']=train_data['notRepairedDamage'].astype('float64')#转成64位的\n",
    "test_data['notRepairedDamage']=test_data['notRepairedDamage'].astype('float64')#转成64位的\n",
    "train_data.info()#可以看到数据类型已经改变了  也就是说train_data里面已经全是数值类型了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 50000 entries, 0 to 49999\n",
      "Data columns (total 33 columns):\n",
      " #   Column             Non-Null Count  Dtype  \n",
      "---  ------             --------------  -----  \n",
      " 0   SaleID             50000 non-null  int64  \n",
      " 1   name               50000 non-null  int64  \n",
      " 2   regDate            50000 non-null  int64  \n",
      " 3   model              50000 non-null  float64\n",
      " 4   brand              50000 non-null  int64  \n",
      " 5   bodyType           48496 non-null  float64\n",
      " 6   fuelType           47076 non-null  float64\n",
      " 7   gearbox            48032 non-null  float64\n",
      " 8   power              50000 non-null  int64  \n",
      " 9   kilometer          50000 non-null  float64\n",
      " 10  notRepairedDamage  50000 non-null  float64\n",
      " 11  regionCode         50000 non-null  int64  \n",
      " 12  seller             50000 non-null  int64  \n",
      " 13  offerType          50000 non-null  int64  \n",
      " 14  creatDate          50000 non-null  int64  \n",
      " 15  v_0                50000 non-null  float64\n",
      " 16  v_1                50000 non-null  float64\n",
      " 17  v_2                50000 non-null  float64\n",
      " 18  v_3                50000 non-null  float64\n",
      " 19  v_4                50000 non-null  float64\n",
      " 20  v_5                50000 non-null  float64\n",
      " 21  v_6                50000 non-null  float64\n",
      " 22  v_7                50000 non-null  float64\n",
      " 23  v_8                50000 non-null  float64\n",
      " 24  v_9                50000 non-null  float64\n",
      " 25  v_10               50000 non-null  float64\n",
      " 26  v_11               50000 non-null  float64\n",
      " 27  v_12               50000 non-null  float64\n",
      " 28  v_13               50000 non-null  float64\n",
      " 29  v_14               50000 non-null  float64\n",
      " 30  regTime            46264 non-null  float64\n",
      " 31  creatTime          50000 non-null  int64  \n",
      " 32  usedTime           46264 non-null  float64\n",
      "dtypes: float64(23), int64(10)\n",
      "memory usage: 12.6 MB\n"
     ]
    }
   ],
   "source": [
    "test_data.info()#这个数据类型也改变了 这个里面也全是数值类型了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SaleID                   0\n",
       "name                     0\n",
       "regDate                  0\n",
       "model                    1\n",
       "brand                    0\n",
       "bodyType              4506\n",
       "fuelType              8680\n",
       "gearbox               5981\n",
       "power                    0\n",
       "kilometer                0\n",
       "notRepairedDamage        0\n",
       "regionCode               0\n",
       "seller                   0\n",
       "offerType                0\n",
       "creatDate                0\n",
       "price                    0\n",
       "v_0                      0\n",
       "v_1                      0\n",
       "v_2                      0\n",
       "v_3                      0\n",
       "v_4                      0\n",
       "v_5                      0\n",
       "v_6                      0\n",
       "v_7                      0\n",
       "v_8                      0\n",
       "v_9                      0\n",
       "v_10                     0\n",
       "v_11                     0\n",
       "v_12                     0\n",
       "v_13                     0\n",
       "v_14                     0\n",
       "regTime              11347\n",
       "creatTime                0\n",
       "usedTime             11347\n",
       "dtype: int64"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#看一下train_data里面是否有确实值\n",
    "train_data.isna().sum()#从这里面看出 里面还是有很多缺失值的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3特征和标签选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['SaleID', 'name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType', 'gearbox', 'power', 'kilometer', 'notRepairedDamage', 'regionCode', 'seller', 'offerType', 'creatDate', 'price', 'v_0', 'v_1', 'v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14', 'regTime', 'creatTime', 'usedTime'] 34\n"
     ]
    }
   ],
   "source": [
    "columns_list=train_data.columns.tolist()\n",
    "print(columns_list,len(columns_list))#这样就拿到了 列名的索引 一共有34个 全是数值类型的格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['name', 'regDate', 'model', 'brand', 'bodyType', 'fuelType', 'gearbox', 'power', 'kilometer', 'notRepairedDamage', 'regionCode', 'seller', 'offerType', 'creatDate', 'v_0', 'v_1', 'v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14', 'regTime', 'creatTime', 'usedTime'] 32\n"
     ]
    }
   ],
   "source": [
    "#我们要进行模型预测  SaleID 和 price 是我们不需要的 其他的特征是我们需要拿到的\n",
    "features=[i for i in columns_list if i not in ['SaleID','price']]\n",
    "print(features,len(features))#跟预期的一样  32个特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150000, 32)\n",
      "(150000,)\n",
      "(50000, 32)\n"
     ]
    }
   ],
   "source": [
    "#有了这个  我们就可以拿到要训练的 X和y了\n",
    "X_data=train_data[features]\n",
    "Y_data=train_data['price']\n",
    "X_test=test_data[features]\n",
    "print(X_data.shape)\n",
    "print(Y_data.shape)\n",
    "print(X_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义一个统计函数方便了解数据的分布\n",
    "def show_stats(data):\n",
    "    \"\"\"data是输入进来的数据，然后求出下面的特征\"\"\"\n",
    "    print(\"最小值：{}\".format(np.min(data)))\n",
    "    print(\"最大值：{}\".format(np.max(data)))\n",
    "    print(\"最大小值之差：{}\".format(np.ptp(data)))\n",
    "    print(\"均值：{}\".format(np.mean(data)))\n",
    "    print(\"标准差：{}\".format(np.std(data)))\n",
    "    print(\"方差：{}\".format(np.var(data)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         1850\n",
       "1         3600\n",
       "2         6222\n",
       "3         2400\n",
       "4         5200\n",
       "          ... \n",
       "149995    5900\n",
       "149996    9500\n",
       "149997    7500\n",
       "149998    4999\n",
       "149999    4700\n",
       "Name: price, Length: 150000, dtype: int64"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#先拿train_data里面的数据小试牛刀\n",
    "Y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值：11\n",
      "最大值：99999\n",
      "最大小值之差：99988\n",
      "均值：5923.327333333334\n",
      "标准差：7501.973469876438\n",
      "方差：56279605.94272992\n"
     ]
    }
   ],
   "source": [
    "show_stats(Y_data)#我靠 汽车还有卖11块钱的 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SaleID               0\n",
      "name                 0\n",
      "regDate              0\n",
      "model                0\n",
      "brand                0\n",
      "bodyType             0\n",
      "fuelType             0\n",
      "gearbox              0\n",
      "power                0\n",
      "kilometer            0\n",
      "notRepairedDamage    0\n",
      "regionCode           0\n",
      "seller               0\n",
      "offerType            0\n",
      "creatDate            0\n",
      "price                0\n",
      "v_0                  0\n",
      "v_1                  0\n",
      "v_2                  0\n",
      "v_3                  0\n",
      "v_4                  0\n",
      "v_5                  0\n",
      "v_6                  0\n",
      "v_7                  0\n",
      "v_8                  0\n",
      "v_9                  0\n",
      "v_10                 0\n",
      "v_11                 0\n",
      "v_12                 0\n",
      "v_13                 0\n",
      "v_14                 0\n",
      "regTime              0\n",
      "creatTime            0\n",
      "usedTime             0\n",
      "dtype: int64 SaleID               0\n",
      "name                 0\n",
      "regDate              0\n",
      "model                0\n",
      "brand                0\n",
      "bodyType             0\n",
      "fuelType             0\n",
      "gearbox              0\n",
      "power                0\n",
      "kilometer            0\n",
      "notRepairedDamage    0\n",
      "regionCode           0\n",
      "seller               0\n",
      "offerType            0\n",
      "creatDate            0\n",
      "v_0                  0\n",
      "v_1                  0\n",
      "v_2                  0\n",
      "v_3                  0\n",
      "v_4                  0\n",
      "v_5                  0\n",
      "v_6                  0\n",
      "v_7                  0\n",
      "v_8                  0\n",
      "v_9                  0\n",
      "v_10                 0\n",
      "v_11                 0\n",
      "v_12                 0\n",
      "v_13                 0\n",
      "v_14                 0\n",
      "regTime              0\n",
      "creatTime            0\n",
      "usedTime             0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#刚才里面的数据里面还是有大量缺失值的  需要补全一下\n",
    "train_data=train_data.fillna(0.0)\n",
    "test_data=test_data.fillna(0.0)\n",
    "print(train_data.isna().sum(),test_data.isna().sum())#这里已经没有缺失值了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4模型预测xgboost和lightgbm单独预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "#用模型去预测一下 \n",
    "#先写一个函数 输入model 输出y_predict\n",
    "def model_predict(model):\n",
    "    print(\"使用的模型：\",model)\n",
    "    model.fit(X_data,Y_data)\n",
    "    y_predict=model.predict(X_test)\n",
    "    show_stats(y_predict)\n",
    "    return y_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用的模型： XGBRegressor(base_score=None, booster=None, colsample_bylevel=None,\n",
      "             colsample_bynode=None, colsample_bytree=None, gamma=None,\n",
      "             gpu_id=None, importance_type='gain', interaction_constraints=None,\n",
      "             learning_rate=0.1, max_delta_step=None, max_depth=10,\n",
      "             min_child_weight=None, missing=nan, monotone_constraints=None,\n",
      "             n_estimator=150, n_estimators=100, n_jobs=None,\n",
      "             num_parallel_tree=None, objective='reg:squarederror',\n",
      "             random_state=None, reg_alpha=None, reg_lambda=None,\n",
      "             scale_pos_weight=None, subsample=None, tree_method=None,\n",
      "             validate_parameters=None, verbosity=None)\n",
      "[16:01:14] WARNING: ../src/learner.cc:516: \n",
      "Parameters: { n_estimator } might not be used.\n",
      "\n",
      "  This may not be accurate due to some parameters are only used in language bindings but\n",
      "  passed down to XGBoost core.  Or some parameters are not used but slip through this\n",
      "  verification. Please open an issue if you find above cases.\n",
      "\n",
      "\n",
      "最小值：-122.67103576660156\n",
      "最大值：90703.0\n",
      "最大小值之差：90825.671875\n",
      "均值：5903.9111328125\n",
      "标准差：7334.99169921875\n",
      "方差：53802104.0\n"
     ]
    }
   ],
   "source": [
    "#先试一下 xgboost\n",
    "import xgboost as xgb\n",
    "model_xgb=xgb.XGBRegressor(n_estimator=150,learning_rate=0.1,max_depth=10)\n",
    "pre_xgb=model_predict(model_xgb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用的模型： LGBMRegressor(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,\n",
      "              importance_type='split', learning_rate=0.1, max_depth=-1,\n",
      "              min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,\n",
      "              n_estimators=150, n_jobs=-1, num_leaves=127, objective=None,\n",
      "              random_state=None, reg_alpha=0.0, reg_lambda=0.0, silent=True,\n",
      "              subsample=1.0, subsample_for_bin=200000, subsample_freq=0)\n",
      "最小值：-810.9047305034136\n",
      "最大值：93537.24105464488\n",
      "最大小值之差：94348.1457851483\n",
      "均值：5905.947116724919\n",
      "标准差：7355.657224409989\n",
      "方差：54105693.203014866\n"
     ]
    }
   ],
   "source": [
    "#试一下 lightgbm\n",
    "import lightgbm as lgb\n",
    "model_lgb=lgb.LGBMRegressor(n_estimators=150,num_leaves=127)\n",
    "pre_lgb=model_predict(model_lgb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(105000, 32)\n",
      "(45000, 32)\n",
      "(105000,)\n",
      "(45000,)\n"
     ]
    }
   ],
   "source": [
    "#这样只看到结果 看不到分数的   我们把X_data再切分成训练集和测试集 做一下MAE 看一下效果 然后通过MAE融合一下两个模型 \n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "x_train,x_test,y_train,y_test=train_test_split(X_data,Y_data,test_size=0.3)\n",
    "print(x_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_train.shape)\n",
    "print(y_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5模型(xgboost和lightgbm按照MAE融合)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先写一个函数 输入model 输出y_predict、训练好的model、还有MAE\n",
    "from sklearn.metrics import mean_absolute_error\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "#先写一个函数 输入model 输出网格搜索好的model、还有MAE\n",
    "def model_train(model):\n",
    "    #设置超参数的范围\n",
    "#     param_grid={'learning_rate':[0.01,0.05,0.1,0.2]}\n",
    "#     model=GridSearchCV(model,param_grid)\n",
    "    #模型开始训练\n",
    "    model.fit(x_train,y_train)\n",
    "    y_predict=model.predict(x_test)#预测的值这里不用返回了吧\n",
    "    show_stats(y_predict)#这里显示以预测的效果\n",
    "#     model=model.best_estimator_#这里把网格搜索的最佳参数拿到\n",
    "    MAE=mean_absolute_error(y_test,y_predict)#求一下MAE\n",
    "    print('MAE:',MAE)\n",
    "    return model,MAE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[16:01:50] WARNING: ../src/learner.cc:516: \n",
      "Parameters: { n_estimator } might not be used.\n",
      "\n",
      "  This may not be accurate due to some parameters are only used in language bindings but\n",
      "  passed down to XGBoost core.  Or some parameters are not used but slip through this\n",
      "  verification. Please open an issue if you find above cases.\n",
      "\n",
      "\n",
      "最小值：-290.9076232910156\n",
      "最大值：87668.9140625\n",
      "最大小值之差：87959.8203125\n",
      "均值：5908.77880859375\n",
      "标准差：7331.77001953125\n",
      "方差：53754852.0\n",
      "MAE: 601.0343298485664\n",
      "XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n",
      "             colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,\n",
      "             importance_type='gain', interaction_constraints='',\n",
      "             learning_rate=0.300000012, max_delta_step=0, max_depth=10,\n",
      "             min_child_weight=1, missing=nan, monotone_constraints='()',\n",
      "             n_estimator=150, n_estimators=100, n_jobs=0, num_parallel_tree=1,\n",
      "             objective='reg:squarederror', random_state=0, reg_alpha=0,\n",
      "             reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method='exact',\n",
      "             validate_parameters=1, verbosity=None)\n"
     ]
    }
   ],
   "source": [
    "model_xgb2=xgb.XGBRegressor(n_estimator=150,max_depth=10)\n",
    "model_xgb2,MAE_xgb2=model_train(model_xgb2)\n",
    "print(model_xgb2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] Unknown parameter: n_estimator\n",
      "最小值：-2095.995923261256\n",
      "最大值：91026.05871738188\n",
      "最大小值之差：93122.05464064314\n",
      "均值：5918.842810443691\n",
      "标准差：7338.434032949508\n",
      "方差：53852614.05595159\n",
      "MAE: 591.0961658396542\n",
      "LGBMRegressor(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,\n",
      "              importance_type='split', learning_rate=0.125, max_depth=-1,\n",
      "              min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,\n",
      "              n_estimator=150, n_estimators=100, n_jobs=-1, num_leaves=150,\n",
      "              objective=None, random_state=None, reg_alpha=0.0, reg_lambda=0.0,\n",
      "              silent=True, subsample=1.0, subsample_for_bin=200000,\n",
      "              subsample_freq=0)\n"
     ]
    }
   ],
   "source": [
    "model_lgb2=lgb.LGBMRegressor(n_estimator=150,learning_rate=0.125,num_leaves=150)\n",
    "model_lgb2,MAE_lgb2=model_train(model_lgb2)\n",
    "print(model_lgb2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "XGB权重：0.4958317633661511\n",
      "LGB权重：0.5041682366338489\n"
     ]
    }
   ],
   "source": [
    "# MAE_xgb2 和 MAE_lgb2 模型融合\n",
    "weight_xgb=1-MAE_xgb2/(MAE_lgb2+MAE_xgb2)\n",
    "weight_lgb=1-MAE_lgb2/(MAE_lgb2+MAE_xgb2)\n",
    "\n",
    "print('XGB权重：{}'.format( weight_xgb ))\n",
    "print('LGB权重：{}'.format( weight_lgb))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值：-483.1282043457031\n",
      "最大值：91388.65625\n",
      "最大小值之差：91871.78125\n",
      "均值：5896.33349609375\n",
      "标准差：7324.23095703125\n",
      "方差：53644360.0\n"
     ]
    }
   ],
   "source": [
    "#用上面的模型分别去预测 得到y的值  这次用全部的值 X_test\n",
    "y_xgb2=model_xgb2.predict(X_test)\n",
    "show_stats(y_xgb2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值：11\n",
      "最大值：99999\n",
      "最大小值之差：99988\n",
      "均值：5923.327333333334\n",
      "标准差：7501.973469876438\n",
      "方差：56279605.94272992\n"
     ]
    }
   ],
   "source": [
    "show_stats(Y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值：-1018.2242457605199\n",
      "最大值：89361.85933987932\n",
      "最大小值之差：90380.08358563983\n",
      "均值：5908.0819858345985\n",
      "标准差：7361.343467496598\n",
      "方差：54189377.64645484\n"
     ]
    }
   ],
   "source": [
    "y_lgb2=model_lgb2.predict(X_test)\n",
    "show_stats(y_lgb2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:8: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "#结果融合\n",
    "predict_y_xlgb=y_xgb2*weight_xgb+y_lgb2*weight_lgb#模型按照权重融合\n",
    "\n",
    "result_xlgb=pd.DataFrame()#做一个新的DF\n",
    "result_xlgb['SaleID']=test_data['SaleID']#把SaleID放进去\n",
    "result_xlgb['price']=predict_y_xlgb#把预测的结果也放进去\n",
    "#把预测的小于11的值改为11\n",
    "result_xlgb[result_xlgb['price']<11]['price']=11\n",
    "#输出结果\n",
    "result_xlgb.to_csv('xlGB.csv',index=False)#输出成文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6提交比赛结果\n",
    "把输出的文件提交到天池，然后会有一个分数。（根据经验，模型融合确实有一定的提升）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
